﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Wizard.Runtime
{
    public class RuntimeEngine : Wizard.IRuntimeEngine
    {
        private readonly List<Plugins.LoadedPlugin> mPlugins;
        private readonly List<Exception> mErrors;

        public RuntimeEngine()
        {
            mPlugins = new List<Wizard.Runtime.Plugins.LoadedPlugin>();
            mErrors = new List<Exception>();
        }

        #region IRuntimeEngine Members

        IPlugin[] IRuntimeEngine.LoadedPlugins
        {
            get
            {
                List<IPlugin> result = new List<IPlugin>();
                foreach (var loadedPlugin in mPlugins)
                {
                    result.Add(loadedPlugin.mPlugin);
                }
                return result.ToArray();
            }
        }

        void IRuntimeEngine.RegisterPlugin(IPlugin newPlugin)
        {
            try
            {
                newPlugin.Load(); // in case of any error we don't add the plugin.
                mPlugins.Add(new Plugins.LoadedPlugin(newPlugin));
            }
            catch (Exception ex)
            {
                mErrors.Add(ex);
            }
        }

        #endregion

        #region ICompilerEngine Members

        void ICompilerEngine.Compile(Wizard.Source.IWizardApplication application)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IPlugin Members

        string IPlugin.FriendlyName
        {
            get { throw new NotImplementedException(); }
        }

        void IPlugin.Load()
        {
        }

        void IPlugin.Initialize(IWizardEngine engine)
        {
            // statements?

        }

        void IRuntimeEngine.InitializePlugins(IWizardEngine engine)
        {
            foreach (var m in mPlugins)
            {
                m.mPlugin.Initialize(engine);
            }
            
        }

        #endregion
    }
}
